#include <Game\fwCameraLookat.h>
#include <Game\fwCameraSimple.h>
#include "MyGame.h"
#include "StateLogo.h"
#include "Global.h"
#include "Friend.h"

MyGame::MyGame()
{
	LOGIDBG("Init MyGame\n");
}

MyGame::~MyGame()
{
	LOGIDBG("Destroy MyGame\n");
}

void MyGame::test(const FWobject, FWobject)
{}

FWint MyGame::Init(FWuint id, FWuint iWidth, FWuint iHeight, char* szTitle, FWuint iFlags)
{
	FWint err = 0;
	err += IGame::Init(id, iWidth, iHeight, szTitle, iFlags);

	//glEnablede
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glEnable(GL_DEPTH_BITS);
	glEnable(GL_DEPTH_TEST);


	//Camera config
	CameraLookat *lookat = new CameraLookat();
	lookat->Position = Vector3(0.0f, 0.0f, 5.0f);
	lookat->Target = Vector3(0.0f, 0.0f, 0.0f);
	lookat->Up = Vector3(0.0f, 1.0f, 0.0f);
	Global::CurrentCamera = lookat;

	//Message service config
	Global::MSGService = MessageService::GetInstance();
	Global::MSGService->RegisterMessage(Global::CurrentState, "KEYBOARD_EVENT", CallKeyBoard);
	Global::MSGService->RegisterMessage(Global::CurrentState, "KEYBOARD_EVENT", CallKeyBoard);

	TagControll::Alloc();

	//State manager config
	Global::StateMGR = StateManager::GetInstance();
	Global::StateMGR->RegisterState("logo", new StateLogo);
	Global::StateMGR->Switch("logo");
	Global::StateMGR->ExternalSwitch();

	return err;
}

void MyGame::Destroy()
{
	if (Global::CurrentState != NULL)
		Global::CurrentState->Destroy();
	SAFE_DEL(Global::CurrentCamera);
	StateManager::DestroyInstance();

	IGame::Destroy();
}

void MyGame::Update(FWfloat deltaTime)
{
	//Update camera
	if (Global::CurrentCamera)
		Global::CurrentCamera->Update(deltaTime);

	//Set current state
	Global::CurrentState = Global::StateMGR->GetCurrentState();

	//Update current state
	if (Global::CurrentState != NULL)
		Global::CurrentState->Update(deltaTime);
}

void MyGame::Draw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	if (Global::CurrentState != NULL)
		Global::CurrentState->Draw();
}